package main

import "fmt"

import "database/sql"
import (
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	//db, _ := sql.Open("mysql", "dev:dev@/gong2game")
	//
	//db, err := sql.Open("mysql", "dev:dev@tcp(localhost:3306)/gong2game?charset=utf8")
	//fmt.Println(err)
	//
	//rows, err := db.Query("SELECT id,uid,lv,sex FROM AvatarModel")
	//
	//for rows.Next() {
	//	var id int
	//	var uid string
	//	var lv int
	//	var sex int
	//	rows.Columns()
	//	err = rows.Scan(&id, &uid, &lv, &sex)
	//	//fmt.Println(id)
	//	//fmt.Println(uid)
	//	//fmt.Println(lv)
	//	fmt.Println(err)
	//}
	//fmt.Println(rows)
	//fmt.Println(rows.Columns())
	//test1()
	//test2()
	//truncateTable("BuildingModel")
	db, err := sql.Open("mysql", "dev:dev@tcp(localhost:3306)/gong2game?charset=utf8")
	fmt.Println(err)
	test1(*db)
}


func test1(db sql.DB) {


	rows, _ := db.Query("show tables")

	//rows, err := db.Query("SELECT * FROM user limit 1")
	defer rows.Close()

	columns, _ := rows.Columns()
	scanArgs := make([]interface{}, len(columns))
	values := make([]interface{}, len(columns))
	for j := range values {
		scanArgs[j] = &values[j]
	}

	record := make(map[string]string)
	for rows.Next() {
		//将行数据保存到record字典
		 rows.Scan(scanArgs...)
		for i, col := range values {
			if col != nil {
				record[columns[i]] = string(col.([]byte))
				fmt.Print(string(col.([]byte)))
				truncateTable(db,string(col.([]byte)))


			}
		}
	}

	fmt.Println(record)
}


func test2() {
	db, _:= sql.Open("mysql", "dev:dev@tcp(localhost:3306)/gong2game?charset=utf8")
	a, _ := db.Prepare(`TRUNCATE ?`)
	a.Exec()
	fmt.Print(a)
}

func truncateTable(db sql.DB,tableName string) {
	//db, _:= sql.Open("mysql", "dev:dev@tcp(localhost:3306)/gong2game?charset=utf8")
	a, _ := db.Prepare("TRUNCATE "+tableName)
	a.Exec()
	fmt.Print(a)
}
